package com.rc.evcharger.service;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rc.evcharger.common.base.TResponse;
import com.rc.evcharger.common.config.RouterConfig;
import com.rc.evcharger.common.enums.CommanderEnum;
import com.rc.evcharger.model.elec.ElecPile;
import com.rc.evcharger.model.elec.ElecPileHost;
import com.rc.evcharger.service.elec.ElecPileHostService;
import com.rc.evcharger.service.elec.ElecPileService;
import com.rc.evcharger.vo.cec.StdRequest;
import com.rc.evcharger.vo.charge.CommandStartReq;
import com.rc.evcharger.vo.charge.CommandStopReq;
import com.rc.evcharger.utils.RestTemplateUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

/**
 * Create by Yang on 2021/2/19
 */
@Service
public class RouterChargeServiceImpl implements RouterChargeService {
    private static Logger logger = LogManager.getLogger(RouterChargeServiceImpl.class);

    @Autowired
    private ElecPileHostService elecPileHostService;

    @Autowired
    private ElecPileService elecPileService;

    @Autowired
    private RestTemplateUtil restTemplateUtil;

    /**
     * @param tRequest
     * @param elecPileId
     * @param orderSource;//订单来源 0 公众号 1 APP  2小程序 3.刷卡 4.VIN码充电   9.第三方API  默认0
     * @return
     */
    @Override
    public TResponse router_query_start_charge(StdRequest tRequest, Long elecPileId, String orderSource) {
        ElecPile elecPile = elecPileService.selectByPrimaryKey(elecPileId);

        //找到桩对应路由
        ElecPileHost elecPileHost = elecPileHostService.selectByPrimaryKey(elecPileId);
        if (elecPileHost == null) {
            return TResponse.FAIL("路由表找不到桩：{}" + elecPile.getPileNo());
        }

        //寻址
        String requestUrl = "http://" + elecPileHost.getApplicationName() + RouterConfig.APPLICATION_PATH;
        if (CommanderEnum.APP.getCode().equals(orderSource)) {
            //APP接口地址
            requestUrl = requestUrl + RouterConfig.APP_START_CHARGE_PATH;
        }else {
            //其他默认公众号接口地址
            requestUrl = requestUrl + RouterConfig.WECHAT_START_CHARGE_PATH;
        }

        ObjectMapper objectMapper = new ObjectMapper();
        String respData = null;
        try {
            respData = objectMapper.writeValueAsString(tRequest);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        JSONObject tRequestJson = JSONObject.parseObject(respData);

        logger.info("std入参加密接口,启动充电路由被调用，...............对应url:{},orderSource:{}", requestUrl, orderSource);

        //通过rest调用远程服务接口
        ResponseEntity<TResponse> request = restTemplateUtil.request(requestUrl, HttpMethod.POST, tRequestJson, TResponse.class);
        logger.info("std入参加密接口,启动充电路由被调用，...............返回状态码:{},结果集:{}", request.getStatusCode(), request.getBody().toString());
        return request.getBody();
    }

    @Override
    public TResponse router_query_stop_charge(StdRequest tRequest, Long elecPileId, String orderSource) {
        ElecPile elecPile = elecPileService.selectByPrimaryKey(elecPileId);

        //找到桩对应路由
        ElecPileHost elecPileHost = elecPileHostService.selectByPrimaryKey(elecPileId);
        if (elecPileHost == null) {
            return TResponse.FAIL("路由表找不到桩：{}" + elecPile.getPileNo());
        }

        //寻址
        String requestUrl = "http://" + elecPileHost.getApplicationName() + RouterConfig.APPLICATION_PATH;
        if (CommanderEnum.APP.getCode().equals(orderSource)) {
            //APP接口地址
            requestUrl = requestUrl + RouterConfig.APP_STOP_CHARGE_PATH;
        } else {
            //其他默认公众号接口地址
            requestUrl = requestUrl + RouterConfig.WECHAT_STOP_CHARGE_PATH;
        }

        ObjectMapper objectMapper = new ObjectMapper();
        String respData = null;
        try {
            respData = objectMapper.writeValueAsString(tRequest);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        JSONObject tRequestJson = JSONObject.parseObject(respData);

        logger.info("std入参加密接口,停止充电路由被调用，...............对应url:{},orderSource:{}", requestUrl, orderSource);

        //通过rest调用远程服务接口
        ResponseEntity<TResponse> request = restTemplateUtil.request(requestUrl, HttpMethod.POST, tRequestJson, TResponse.class);
        logger.info("std入参加密接口,停止充电路由被调用，...............返回状态码:{},结果集:{}", request.getStatusCode(), request.getBody().toString());
        return request.getBody();
    }

    @Override
    public TResponse evcs_router_query_start_charge(CommandStartReq commandStartReq, Long elecPileId, String orderSource) {
        ElecPile elecPile = elecPileService.selectByPrimaryKey(elecPileId);

        //找到桩对应路由
        ElecPileHost elecPileHost = elecPileHostService.selectByPrimaryKey(elecPileId);
        if (elecPileHost == null) {
            return TResponse.FAIL("stdhlht路由表找不到桩：{}" + elecPile.getPileNo());
        }

        //寻址
        String requestUrl = "http://" + elecPileHost.getApplicationName() + RouterConfig.APPLICATION_PATH;
        if (CommanderEnum.APP.getCode().equals(orderSource)) {
            //APP接口地址
            requestUrl = requestUrl + RouterConfig.APP_START_CHARGE_PATH;
        }else {
            //其他默认公众号接口地址
            requestUrl = requestUrl + RouterConfig.WECHAT_START_CHARGE_PATH;
        }

        ObjectMapper objectMapper = new ObjectMapper();
        String respData = null;
        try {
            respData = objectMapper.writeValueAsString(commandStartReq);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        JSONObject tRequestJson = JSONObject.parseObject(respData);

        logger.info("stdhlht入参加密接口,启动充电路由被调用，...............对应url:{},orderSource:{}", requestUrl, orderSource);

        //通过rest调用远程服务接口
        ResponseEntity<TResponse> request = restTemplateUtil.request(requestUrl, HttpMethod.POST, tRequestJson, TResponse.class);
        logger.info("stdhlht入参加密接口,启动充电路由被调用，...............返回状态码:{},结果集:{}", request.getStatusCode(), request.getBody().toString());
        return request.getBody();
    }

    @Override
    public TResponse evcs_router_query_stop_charge(CommandStopReq commandStopReq, Long elecPileId, String orderSource) {
        ElecPile elecPile = elecPileService.selectByPrimaryKey(elecPileId);

        //找到桩对应路由
        ElecPileHost elecPileHost = elecPileHostService.selectByPrimaryKey(elecPileId);
        if (elecPileHost == null) {
            return TResponse.FAIL("stdhlht路由表找不到桩：{}" + elecPile.getPileNo());
        }

        //寻址
        String requestUrl = "http://" + elecPileHost.getApplicationName() + RouterConfig.APPLICATION_PATH;
        if (CommanderEnum.APP.getCode().equals(orderSource)) {
            //APP接口地址
            requestUrl = requestUrl + RouterConfig.APP_STOP_CHARGE_PATH;
        }else {
            //其他默认公众号接口地址
            requestUrl = requestUrl + RouterConfig.WECHAT_STOP_CHARGE_PATH;
        }

        ObjectMapper objectMapper = new ObjectMapper();
        String respData = null;
        try {
            respData = objectMapper.writeValueAsString(commandStopReq);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        JSONObject tRequestJson = JSONObject.parseObject(respData);

        logger.info("stdhlht入参加密接口,停止充电路由被调用，...............对应url:{},orderSource:{}", requestUrl, orderSource);

        //通过rest调用远程服务接口
        ResponseEntity<TResponse> request = restTemplateUtil.request(requestUrl, HttpMethod.POST, tRequestJson, TResponse.class);
        logger.info("stdhlht入参加密接口,停止充电路由被调用，...............返回状态码:{},结果集:{}", request.getStatusCode(), request.getBody().toString());
        return request.getBody();
    }
}
